using GameFrameWork;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;

namespace GameFrameWork
{

    [DisallowMultipleComponent]
    public class UIOvalMesh : UIBaseMesh
    {
        [SerializeField] Vector2 pivot = Vector2.zero;
        [SerializeField] Vector2 shalf = new(100, 100);
        [SerializeField] int segmentValue = 30;

        float delta_vec_w;
        float delta_vec_h;
        float delta_uv_w;
        float delta_uv_h;


        public Vector2 Pivot { get => pivot; set { pivot = value; graphic?.SetVerticesDirty(); } }
        public Vector2 Shalf { get => shalf; set { shalf = value; graphic?.SetVerticesDirty(); } }

        protected override void OnModifyMesh(VertexHelper vh, ref Vector4 outBound, ref Vector4 outUV, ref Color outColor)
        {
            base.OnModifyMesh(vh, ref outBound, ref outUV, ref outColor);

            vh.Clear();
            {
                Vector2 tex_orign = outBound;

                delta_vec_w = outBound.z - outBound.x;
                delta_vec_h = outBound.w - outBound.y;

                delta_uv_w = outUV.z - outUV.x;
                delta_uv_h = outUV.w - outUV.y;

                float rectSizeX = graphic.rectTransform.rect.size.x;
                float rectSizeY = graphic.rectTransform.rect.size.y;

                float angle = Mathf.PI * 2 / segmentValue;
                float deltaAngle = 0;

                shalf.x = shalf.x > rectSizeX ? rectSizeX : shalf.x;
                shalf.y = shalf.y > rectSizeY ? rectSizeY : shalf.y;

                for (int i = 0, length = segmentValue + 1; i < length; i++)
                {
                    UIVertex point = UIVertex.simpleVert;

                    if (i == length - 1)
                    {
                        point.position = pivot;
                    }
                    else
                    {
                        point.position.x = pivot.x + shalf.x * Mathf.Cos(deltaAngle) * 0.5f;
                        point.position.y = pivot.y + shalf.y * Mathf.Sin(deltaAngle) * 0.5f;

                        deltaAngle += angle;
                    }

                    if (point.position.y > outBound.w)
                    {
                        point.position.y = outBound.w;
                    }

                    if (point.position.y < outBound.y)
                    {
                        point.position.y = outBound.y;
                    }

                    if (point.position.x < outBound.x)
                    {
                        point.position.x = outBound.x;
                    }

                    if (point.position.x > outBound.z)
                    {
                        point.position.x = outBound.z;
                    }

                    float tex_x = (point.position.x - tex_orign.x) / delta_vec_w;
                    float tex_y = (point.position.y - tex_orign.y) / delta_vec_h;

                    point.uv0.x = tex_x * delta_uv_w + outUV.x;
                    point.uv0.y = tex_y * delta_uv_h + outUV.y;

                    point.color = outColor;

                    vh.AddVert(point);
                }

                for (int i = 0, length = segmentValue + 1; i < length; i++)
                {
                    vh.AddTriangle(i, length - 1, (i + 1) % segmentValue);
                }
            }
        }

    }
}

